package com.xyh.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.dao.PermissionDao;
import com.xyh.dao.UserDetailDao;
import com.xyh.dao.UserMapper;
import com.xyh.po.Permission;
import com.xyh.po.User;
import com.xyh.po.UserDetail;
import com.xyh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:kyle
 * @Date:2020/12/17 - 16:20
 * @Project:spring_demo1
 * @Package:com.xyh.service.impl
 * @Version:1.0
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService  {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserDetailDao userDetailDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    @Transactional
    public boolean insertUser(User user) {

        UserDetail userDetail = user.getUserDetail();
        int insert = userDetailDao.insert(userDetail);
        if(insert > 0){
            user.setFkUserDetailId(userDetail.getUserDetailId());
            insert = userMapper.insert(user);
        }

        return insert > 0 ? true : false;
    }

    @Override
    public User login(String username, String password) {
        User user = userMapper.findUser(username, password);
        if(user != null){
            List<Permission> permissionsByUser = permissionDao.findPermissionsByUser(user.getUsername());
            user.setPermissions(permissionsByUser);
        }
        return user;
    }
}
